<template>
  <div v-if="isVisible" class="tech-info-modal-overlay" @click="closeModal">
    <div class="tech-info-modal" @click.stop>
      <div class="modal-header">
        <div class="tech-header">
          <div class="tech-icon">
            <span class="tech-emoji">{{ technology?.icon || '🔬' }}</span>
          </div>
          <div class="tech-title">
            <h2 class="tech-name">{{ technology?.name || '未知科技' }}</h2>
            <div class="tech-category">
              <span class="category-badge" :class="technology?.category">
                {{ getCategoryName(technology?.category) }}
              </span>
              <span class="tech-tier">等级 {{ technology?.tier || 1 }}</span>
            </div>
          </div>
        </div>
        
        <button @click="closeModal" class="close-btn">
          <span class="close-icon">✕</span>
        </button>
      </div>
      
      <div class="modal-content">
        <div class="tech-description">
          <h3 class="section-title">科技描述</h3>
          <p class="description-text">
            {{ technology?.description || '暂无描述' }}
          </p>
        </div>
        
        <div class="tech-stats" v-if="technology">
          <div class="stats-grid">
            <div class="stat-item">
              <span class="stat-label">研究成本</span>
              <span class="stat-value">{{ formatNumber(technology.cost) }} 研究点</span>
            </div>
            <div class="stat-item">
              <span class="stat-label">研究时间</span>
              <span class="stat-value">{{ technology.researchTime }} 天</span>
            </div>
            <div class="stat-item">
              <span class="stat-label">科技等级</span>
              <span class="stat-value">{{ technology.tier }}</span>
            </div>
            <div class="stat-item">
              <span class="stat-label">研究状态</span>
              <span class="stat-value" :class="getStatusClass(technology.status)">
                {{ getStatusName(technology.status) }}
              </span>
            </div>
          </div>
        </div>
        
        <div class="tech-requirements" v-if="technology?.requirements?.length">
          <h3 class="section-title">前置科技</h3>
          <div class="requirements-list">
            <div 
              v-for="req in technology.requirements" 
              :key="req.id"
              class="requirement-item"
              :class="{ completed: req.completed }"
            >
              <span class="req-icon">{{ req.completed ? '✅' : '🔒' }}</span>
              <span class="req-name">{{ req.name }}</span>
            </div>
          </div>
        </div>
        
        <div class="tech-effects" v-if="technology?.effects?.length">
          <h3 class="section-title">科技效果</h3>
          <div class="effects-list">
            <div 
              v-for="effect in technology.effects" 
              :key="effect.id"
              class="effect-item"
            >
              <span class="effect-icon">{{ getEffectIcon(effect.type) }}</span>
              <div class="effect-content">
                <span class="effect-name">{{ effect.name }}</span>
                <span class="effect-description">{{ effect.description }}</span>
              </div>
              <span class="effect-value" :class="getEffectValueClass(effect.value)">
                {{ formatEffectValue(effect.value, effect.type) }}
              </span>
            </div>
          </div>
        </div>
        
        <div class="tech-unlocks" v-if="technology?.unlocks?.length">
          <h3 class="section-title">解锁内容</h3>
          <div class="unlocks-grid">
            <div 
              v-for="unlock in technology.unlocks" 
              :key="unlock.id"
              class="unlock-item"
            >
              <div class="unlock-icon">{{ unlock.icon }}</div>
              <div class="unlock-info">
                <span class="unlock-name">{{ unlock.name }}</span>
                <span class="unlock-type">{{ unlock.type }}</span>
              </div>
            </div>
          </div>
        </div>
        
        <div class="tech-progress" v-if="technology?.status === 'researching'">
          <h3 class="section-title">研究进度</h3>
          <div class="progress-info">
            <div class="progress-bar">
              <div 
                class="progress-fill" 
                :style="{ width: `${technology.progress || 0}%` }"
              ></div>
            </div>
            <div class="progress-text">
              <span class="progress-percentage">{{ Math.round(technology.progress || 0) }}%</span>
              <span class="progress-eta">预计完成: {{ getETA(technology) }}</span>
            </div>
          </div>
        </div>
      </div>
      
      <div class="modal-footer">
        <div class="footer-info">
          <span class="tech-id">ID: {{ technology?.id }}</span>
        </div>
        
        <div class="footer-actions">
          <button 
            v-if="canStartResearch" 
            @click="startResearch"
            class="action-btn primary"
          >
            <span class="btn-icon">🚀</span>
            开始研究
          </button>
          
          <button 
            v-if="canAddToQueue" 
            @click="addToQueue"
            class="action-btn secondary"
          >
            <span class="btn-icon">📋</span>
            加入队列
          </button>
          
          <button 
            v-if="technology?.status === 'researching'" 
            @click="pauseResearch"
            class="action-btn warning"
          >
            <span class="btn-icon">⏸️</span>
            暂停研究
          </button>
          
          <button @click="closeModal" class="action-btn">
            关闭
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'

// Props
interface Technology {
  id: string
  name: string
  description: string
  category: string
  tier: number
  cost: number
  researchTime: number
  status: 'available' | 'researching' | 'completed' | 'locked'
  progress?: number
  icon?: string
  requirements?: Array<{
    id: string
    name: string
    completed: boolean
  }>
  effects?: Array<{
    id: string
    name: string
    description: string
    type: string
    value: number | string
  }>
  unlocks?: Array<{
    id: string
    name: string
    type: string
    icon: string
  }>
}

interface Props {
  isVisible: boolean
  technology: Technology | null
}

const props = defineProps<Props>()

// Emits
const emit = defineEmits<{
  close: []
  startResearch: [technology: Technology]
  addToQueue: [technology: Technology]
  pauseResearch: [technology: Technology]
}>()

// Computed
const canStartResearch = computed(() => {
  return props.technology?.status === 'available' && 
         props.technology?.requirements?.every(req => req.completed)
})

const canAddToQueue = computed(() => {
  return props.technology?.status === 'available' || 
         props.technology?.status === 'locked'
})

// Methods
function closeModal() {
  emit('close')
}

function startResearch() {
  if (props.technology && canStartResearch.value) {
    emit('startResearch', props.technology)
  }
}

function addToQueue() {
  if (props.technology && canAddToQueue.value) {
    emit('addToQueue', props.technology)
  }
}

function pauseResearch() {
  if (props.technology) {
    emit('pauseResearch', props.technology)
  }
}

function getCategoryName(category?: string): string {
  const categories: Record<string, string> = {
    physics: '物理学',
    engineering: '工程学',
    society: '社会学',
    military: '军事学',
    economics: '经济学'
  }
  return categories[category || ''] || '未知'
}

function getStatusName(status?: string): string {
  const statuses: Record<string, string> = {
    available: '可研究',
    researching: '研究中',
    completed: '已完成',
    locked: '已锁定'
  }
  return statuses[status || ''] || '未知'
}

function getStatusClass(status?: string): string {
  const classes: Record<string, string> = {
    available: 'status-available',
    researching: 'status-researching',
    completed: 'status-completed',
    locked: 'status-locked'
  }
  return classes[status || ''] || ''
}

function getEffectIcon(type: string): string {
  const icons: Record<string, string> = {
    damage: '⚔️',
    defense: '🛡️',
    speed: '⚡',
    cost: '💰',
    efficiency: '⚙️',
    capacity: '📦',
    range: '🎯',
    production: '🏭'
  }
  return icons[type] || '📈'
}

function getEffectValueClass(value: number | string): string {
  if (typeof value === 'number') {
    return value > 0 ? 'effect-positive' : 'effect-negative'
  }
  return ''
}

function formatEffectValue(value: number | string, type: string): string {
  if (typeof value === 'number') {
    const sign = value > 0 ? '+' : ''
    if (type === 'percentage') {
      return `${sign}${value}%`
    }
    return `${sign}${value}`
  }
  return String(value)
}

function formatNumber(num: number): string {
  if (num >= 1000000) {
    return `${(num / 1000000).toFixed(1)}M`
  } else if (num >= 1000) {
    return `${(num / 1000).toFixed(1)}K`
  }
  return num.toString()
}

function getETA(technology: Technology): string {
  if (!technology.progress || technology.progress === 0) {
    return `${technology.researchTime} 天`
  }
  
  const remainingProgress = 100 - technology.progress
  const remainingDays = Math.ceil((remainingProgress / 100) * technology.researchTime)
  
  if (remainingDays <= 1) {
    return '不到1天'
  } else if (remainingDays <= 30) {
    return `${remainingDays} 天`
  } else {
    const months = Math.ceil(remainingDays / 30)
    return `约 ${months} 个月`
  }
}
</script>

<style scoped>
.tech-info-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 2rem;
}

.tech-info-modal {
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  width: 100%;
  max-width: 800px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.5rem;
  border-bottom: 1px solid var(--border-color);
  background: var(--bg-secondary);
  border-radius: 12px 12px 0 0;
}

.tech-header {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.tech-icon {
  width: 60px;
  height: 60px;
  background: var(--primary-color);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tech-emoji {
  font-size: 2rem;
}

.tech-title {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.tech-name {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--text-primary);
}

.tech-category {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.category-badge {
  padding: 0.25rem 0.75rem;
  border-radius: 20px;
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
}

.category-badge.physics {
  background: #3b82f6;
  color: white;
}

.category-badge.engineering {
  background: #f59e0b;
  color: white;
}

.category-badge.society {
  background: #10b981;
  color: white;
}

.category-badge.military {
  background: #ef4444;
  color: white;
}

.category-badge.economics {
  background: #8b5cf6;
  color: white;
}

.tech-tier {
  font-size: 0.9rem;
  color: var(--text-secondary);
  font-weight: 500;
}

.close-btn {
  width: 40px;
  height: 40px;
  background: none;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}

.close-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.close-icon {
  font-size: 1.2rem;
}

.modal-content {
  flex: 1;
  padding: 1.5rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.section-title {
  margin: 0 0 1rem 0;
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text-primary);
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--border-color);
}

.tech-description {
  background: var(--bg-secondary);
  padding: 1.5rem;
  border-radius: 8px;
  border: 1px solid var(--border-color);
}

.description-text {
  margin: 0;
  line-height: 1.6;
  color: var(--text-primary);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
}

.stat-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem;
  background: var(--bg-secondary);
  border-radius: 6px;
  border: 1px solid var(--border-color);
}

.stat-label {
  color: var(--text-secondary);
  font-weight: 500;
}

.stat-value {
  color: var(--text-primary);
  font-weight: 600;
}

.stat-value.status-available {
  color: var(--success-color);
}

.stat-value.status-researching {
  color: var(--warning-color);
}

.stat-value.status-completed {
  color: var(--info-color);
}

.stat-value.status-locked {
  color: var(--text-secondary);
}

.requirements-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.requirement-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem;
  background: var(--bg-secondary);
  border-radius: 6px;
  border: 1px solid var(--border-color);
}

.requirement-item.completed {
  background: var(--success-bg);
  border-color: var(--success-color);
}

.req-icon {
  font-size: 1.2rem;
}

.req-name {
  color: var(--text-primary);
  font-weight: 500;
}

.effects-list {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.effect-item {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 1rem;
  background: var(--bg-secondary);
  border-radius: 8px;
  border: 1px solid var(--border-color);
}

.effect-icon {
  font-size: 1.5rem;
  width: 40px;
  text-align: center;
}

.effect-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.effect-name {
  font-weight: 600;
  color: var(--text-primary);
}

.effect-description {
  font-size: 0.9rem;
  color: var(--text-secondary);
}

.effect-value {
  font-weight: 700;
  font-size: 1.1rem;
}

.effect-value.effect-positive {
  color: var(--success-color);
}

.effect-value.effect-negative {
  color: var(--error-color);
}

.unlocks-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1rem;
}

.unlock-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 1rem;
  background: var(--bg-secondary);
  border-radius: 8px;
  border: 1px solid var(--border-color);
}

.unlock-icon {
  font-size: 1.5rem;
  width: 40px;
  text-align: center;
}

.unlock-info {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.unlock-name {
  font-weight: 600;
  color: var(--text-primary);
}

.unlock-type {
  font-size: 0.8rem;
  color: var(--text-secondary);
  text-transform: uppercase;
}

.tech-progress {
  background: var(--bg-secondary);
  padding: 1.5rem;
  border-radius: 8px;
  border: 1px solid var(--border-color);
}

.progress-info {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.progress-bar {
  height: 12px;
  background: var(--bg-primary);
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid var(--border-color);
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary-color), var(--primary-hover));
  transition: width 0.3s ease;
}

.progress-text {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.progress-percentage {
  font-weight: 700;
  color: var(--primary-color);
}

.progress-eta {
  color: var(--text-secondary);
  font-size: 0.9rem;
}

.modal-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.5rem;
  border-top: 1px solid var(--border-color);
  background: var(--bg-secondary);
  border-radius: 0 0 12px 12px;
}

.footer-info {
  color: var(--text-secondary);
  font-size: 0.8rem;
}

.footer-actions {
  display: flex;
  gap: 0.75rem;
}

.action-btn {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem 1.5rem;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
}

.action-btn.primary {
  background: var(--primary-color);
  color: white;
  border-color: var(--primary-color);
}

.action-btn.primary:hover {
  background: var(--primary-hover);
}

.action-btn.secondary {
  background: var(--info-color);
  color: white;
  border-color: var(--info-color);
}

.action-btn.secondary:hover {
  background: var(--info-hover);
}

.action-btn.warning {
  background: var(--warning-color);
  color: white;
  border-color: var(--warning-color);
}

.action-btn.warning:hover {
  background: var(--warning-hover);
}

.action-btn:not(.primary):not(.secondary):not(.warning) {
  background: var(--bg-primary);
  color: var(--text-primary);
}

.action-btn:not(.primary):not(.secondary):not(.warning):hover {
  background: var(--bg-hover);
}

.btn-icon {
  font-size: 1rem;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .tech-info-modal-overlay {
    padding: 1rem;
  }
  
  .modal-header {
    padding: 1rem;
  }
  
  .tech-header {
    gap: 0.75rem;
  }
  
  .tech-icon {
    width: 50px;
    height: 50px;
  }
  
  .tech-emoji {
    font-size: 1.5rem;
  }
  
  .tech-name {
    font-size: 1.25rem;
  }
  
  .modal-content {
    padding: 1rem;
  }
  
  .stats-grid {
    grid-template-columns: 1fr;
  }
  
  .unlocks-grid {
    grid-template-columns: 1fr;
  }
  
  .modal-footer {
    flex-direction: column;
    gap: 1rem;
    align-items: stretch;
  }
  
  .footer-actions {
    justify-content: center;
    flex-wrap: wrap;
  }
  
  .action-btn {
    flex: 1;
    min-width: 120px;
  }
}
</style>